/**
 * weiyinli.com Inc.
 * Copyright (c) 2006-2011 All Rights Reserved.
 */
package com.nmp.web.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.nmp.persistence.entity.Demo;
import com.nmp.service.base.result.Result;
import com.nmp.service.demo.DemoService;
import com.nmp.web.controller.base.ControllerSupport;

/**
 * @author defier.lai
 * 2011-9-19 下午10:14:18
 * @version 1.0
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping("/demo")
public class DemoController extends ControllerSupport {
	
	private final String LIST_ACTION = "redirect:/demo";
	@Resource
	private DemoService demoService;
	
	 /** 列表 */  
    @RequestMapping
    public ModelAndView index(HttpServletRequest request,HttpServletResponse response) {  
    	addObject("message", "index ok...哈哈");
    	Demo demo = new Demo();
    	demo.setDate(new Date());
    	demo.setName("百度baidu.com");
    	Result result = demoService.save(demo);
    	System.out.println(result.getResultCode().getMessage());
        return getModelAndView("index");  
    }  
      
    /** 进入新增 */  
    @RequestMapping(value="/add")  
    public ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws Exception {  
        return new ModelAndView("/demo/add","demo", "");  
    }
      
    /** 显示 */  
    @RequestMapping(value="/{id}")  
    public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
//        demo demo = (demo)demoManager.getById(id);  
        return new ModelAndView("/demo/show","demo","");  
    }
      
    /** 编辑 */  
    @RequestMapping(value="/{id}/edit")  
    public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {
    	
        return new ModelAndView("/demo/edit","demo","x");  
    }  
      
    /** 保存新增 */  
    @RequestMapping(method=RequestMethod.POST)  
    public ModelAndView create(HttpServletRequest request,HttpServletResponse response) throws Exception {  
         
        return new ModelAndView(LIST_ACTION);  
    }  
      
    /** 保存更新 */  
    @RequestMapping(value="/{id}",method=RequestMethod.PUT)  
    public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
       /* demo demo = (demo)demoManager.getById(id);  
        bind(request,demo);  
        demoManager.update(demo); */ 
        return new ModelAndView(LIST_ACTION);  
    }  
      
    /** 删除 */  
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)  
    public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {  
//        demoManager.removeById(id);  
        return new ModelAndView(LIST_ACTION);  
    }  
  
    /** 批量删除 */  
    @RequestMapping(method=RequestMethod.DELETE)  
    public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {  
          
        /*for(int i = 0; i < items.length; i++) {  
              
            demoManager.removeById(items[i]);  
        }  */
        return new ModelAndView(LIST_ACTION);  
    }

	@Override
	public String getParentPath() {
		return "/demo";
	} 

}
